﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WISD.CarFriend.Model;
using WISD.Common;
using WISD.CarFriend.BLL.OA;
using System.Data;
using WISD.CarFriend.BLL.Web;

namespace WISD.CarFriend.Web.OA
{
    public partial class RegistCard : System.Web.UI.Page
    {
        #region 事件
        /// <summary>
        /// Page_Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SetToolTip();
                View();
            }
        }


        /// <summary>
        /// 开卡保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                WebModule bll = new WebModule();
                if (bll.ValidMemberCardID(txtMemberCardID.Text.Trim()))
                {
                    string script = "";
                    script += "<script type=\"text/javascript\">\n";
                    script += "parent.jsprint(\"" + "该用户卡号已经注册过！" + "\",\"" + "" + "\",\"" + "Error" + "\")\n";
                    script += "</script>\n";
                    this.Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "JsPrint", script);
                    return;
                }
                string MemberID = txtMemberID.Text.Trim();

                Dictionary<string, string> dictionary = new Dictionary<string, string>();
                dictionary.Add("MemberCardID", this.txtMemberCardID.Text.Trim());
                dictionary.Add("MemberID", MemberID);
                dictionary.Add("MemberLevel", SelMemberLevel.SelectedValue);
                dictionary.Add("AdmissionDate", txtAdmissionDate.Text.Trim());
                dictionary.Add("ExpirationDate", txtExpirationDate.Text.Trim());
                dictionary.Add("Pwd", txtPwd.Text.Trim());
                dictionary.Add("ResidualAmount", txtResidualAmount.Text.Trim());
                dictionary.Add("State", SelState.SelectedValue);
                dictionary.Add("Reviser", Session["UserID"].ToString());
                dictionary.Add("Memo", txtMemo.Text.Trim());
                dictionary.Add("ReviseDate", DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));

                if (BLL.OA.MemberCardModule.AddEntity(dictionary).Equals("Success"))
                {
                    Session["userid"] = MemberID;
                    MessageBox.AlertAndGo(this.Page, this.txtMemberCardID.Text.Trim() + "开卡成功!!!!!", "../../Index.aspx");
                }
                else
                {
                    MessageBox.Alert(this.Page, this.txtMemberCardID.Text.Trim() + "开卡失败!!!!!");
                }
            }
            catch (Exception ex)
            {
            }
        }
        #endregion


        #region 方法
        /// <summary>
        /// 初始化值
        /// </summary>
        private void View()
        {
            if (Session["UserID"] == null)
            {
                MessageBox.AlertAndGo(this.Page, "请先注册会员，然后再开卡！", "Regist.aspx");
                return;
            }
            //txtAdmissionDate.Text = DateTime.Now.ToShortDateString();
            //txtExpirationDate.Text = DateTime.Now.AddYears(1).ToShortDateString();

            txtMemberID.Text = Session["UserID"].ToString();
            txtName.Text = Session["UserName"].ToString();
            txtMobileTel.Text = Request.QueryString["MobileTel"];
            txtVehicleNo.Text = Request.QueryString["VehicleNo"];
            txtVehicleStyle.Text = Request.QueryString["VehicleStyle"];

            MemberCardTypeModule memberCardTypeModule = new MemberCardTypeModule();
            DataSet ds = memberCardTypeModule.GetList("");

            if (ds != null && ds.Tables.Count != 0)
            {
                SelMemberLevel.DataTextField = "MemberCardType";
                SelMemberLevel.DataValueField = "Id";

                SelMemberLevel.DataSource = ds.Tables[0];
                SelMemberLevel.DataBind();
            }
        }

        /// <summary>
        /// 绑定页面控件的ToolTip属性
        /// </summary>
        private void SetToolTip()
        {
            DataSet ds = ToolTipManageModule.GetList(" TableName ='T_CF_OA_MemberCard' ");
            if (ds != null && ds.Tables.Count != 0)
            {
                ControlCollection ControlList = this.form1.Controls;
                foreach (System.Web.UI.Control Controls in ControlList)
                {
                    if (Controls.GetType().Equals(typeof(System.Web.UI.WebControls.TextBox)))
                    {
                        TextBox tb = (TextBox)Controls;

                        string tip = tb.Attributes["tip"];
                        foreach (DataRow r in ds.Tables[0].Rows)
                        {
                            if (tip == r["Markup"].ToString())
                            {
                                tb.Attributes.Add("HintTitle", r["Title"].ToString());
                                tb.Attributes.Add("HintInfo", r["Content"].ToString());
                                break;
                            }
                        }
                    }
                }
            }
        }
        #endregion
    }
}